// pages/submit-order/submit-order.ts
import { formatTime } from '../../utils/util';
import qs from 'qs'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: [],
    price: 0,
    user: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    const cart = qs.parse(options) as unknown as any[]
    const priceArr: number[] = []
      cart[0].forEach((item: any) => {
        const  price = item.count * item.price * 100
        priceArr.push(price)
      });
      let price = 0
      priceArr.forEach((item) => {
        price += item
      })
    this.setData({
      order: cart[0] as never[],
      price,
    })
  },
  onShow() {
    const user = wx.getStorageSync('user')
    this.setData({
      user
    })
  },
  onSubmit() {
    wx.showLoading({
      title: '支付中'
    })
    let order = wx.getStorageSync('order')
    if (!order) order = []
    order.unshift({foods: this.data.order, time: formatTime(new Date())})
    wx.setStorageSync('order', order)
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '支付成功',
        complete: () => {
          const app = getApp()
          app.globalData.isCleanCart = true
          wx.switchTab({
            url: '/pages/home/home'
          })
        }
      })
    }, 2000);
  },
  onTapAddress() {
    wx.navigateTo({
      url: '/pages/address/address'
    })
  }
})